package com.wn.comment.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wn.comment.common.DataGridResponse;
import com.wn.comment.mapper.CommentMapper;
import com.wn.comment.service.CommentService;
import com.wn.comment.vo.CarPortOrCommentVO;
import com.wn.common.entity.MyParam;
import com.wn.common.pojo.CarPort;
import com.wn.common.pojo.Comment;
import com.wn.common.pojo.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: shared_parking
 * @BelongsPackage: com.wn.comment.service.impl
 * @Author: 徐俊柯
 * @CreateTime: 2020-06-11 14:37
 * @Description:
 */
@Service
@Transactional
public class CommentImpl implements CommentService{

    @Autowired
    CommentMapper commentMapper;
    

    /*
     * 查询评价：
     */
    @Transactional(readOnly = true)
    public PageInfo<Comment> getCommentAll(MyParam<Comment> myParam) {
        //查询出所有评价
        if (StringUtil.isEmpty(myParam.getSuperSearchKeyWord())){
            //开启分页
            PageHelper.startPage(myParam.getMyPage().getPageNum(),myParam.getMyPage().getPageSize());
            //获取排序规则的数组
            String[] pageParam = myParam.getPageParam();
            //遍历数组
            for(int i = 0;i < pageParam.length;i++){
                PageHelper.orderBy(pageParam[i]);  //  "ID  DESC" ,"AGE  ASC"
            }
            List<Comment> all = commentMapper.getCommentAll(myParam.getT());
            return new PageInfo<Comment>(all);
        }else {   //高级检索
            //开启分页
            PageHelper.startPage(myParam.getMyPage().getPageNum(),myParam.getMyPage().getPageSize());
            //获取排序规则的数组
            String[] pageParam = myParam.getPageParam();
            //遍历数组
            for(int i = 0;i < pageParam.length;i++){
                PageHelper.orderBy(pageParam[i]);  //  "ID  DESC" ,"AGE  ASC"
            }
            List<Comment> all = commentMapper.superSearch(myParam.getSuperSearchKeyWord());
            return new PageInfo<Comment>(all);
        }
    }

    /*
     * 后台系统管理的平台方查询出所有评价（分页）和模糊查询
     */
    public DataGridResponse getCommentList(Integer page, Integer rows,String coRtostate){
        PageHelper.startPage(page, rows);
        List<Comment> comments = commentMapper.getCommentList(coRtostate);
        PageInfo<Comment> commentsPageInfo = new PageInfo<Comment>(comments);
        return new DataGridResponse((int)commentsPageInfo.getTotal(),comments);
    }

    /*
     *添加一条评价信息
     */
    @Override
    public Integer addOne(String commentName,String coT,String coRtostate) {
        String s = UUID.randomUUID().toString();
        Comment c = new Comment();
        c.setCoId(s);
        Integer integer = commentMapper.addOne(c.getCoId(),commentName,coT,coRtostate);
        return integer;
    }


    /*
     * 删除
     */
    //1.物理删除
    @Override
    public Integer deleteOneTrueById(String coId) {
        Integer integer = commentMapper.deleteOneTrueById(coId);
        return integer;
    }
    //2.逻辑删除
    @Override
    public Integer deleteOneLogicalById(String coId) {
        Integer integer = commentMapper.deleteOneLogicalById(coId);
        return integer;
    }
    //3.恢复一个被逻辑删除的评价
    @Override
    public Integer recoverOneById(String coIds) {
        Integer integer = commentMapper.recoverOneById(coIds);
        return integer;
    }
    //4.批量删除
    @Override
    public Integer deleteSomeByIds(String[] coIds) {
        Integer integer = commentMapper.deleteSomeByIds(coIds);
        return integer;
    }

    /*
     * 手机端前台 根据车位ID查询出订单ID
     * 手机端前台 根据车位ID查询出的车位信息
     */
    @Override
    public List<CarPort> cpIdGetOrderId(String cpId) {
        List<CarPort> carPort = commentMapper.cpIdGetOrderId(cpId);
        return carPort;
    }

    /*
     * 手机端前台 租客方查看所有评价  包括分页功能
     */
    @Override
    public List<CarPortOrCommentVO> getPhoneCommentAll(String oId) {
        List<CarPortOrCommentVO> phoneCommentAll = commentMapper.getPhoneCommentAll(oId);
        return phoneCommentAll;
    }

    /**
     * 根据前端传递过来的用户名称查询出用户的订单ID
     */
    @Override
    public List<Orders> getOrderId(String realName) {
        List<Orders> orderId = commentMapper.getOrderId(realName);
        return orderId;
    }

    /*
     * 手机端前台 添加评价
     */
    @Override
    public Integer addPhoneComment(String oId,String commentName,String coRtostate,String coT) {
        String s = UUID.randomUUID().toString();
        Comment comment = new Comment();
        comment.setCoId(s);
        Integer integer = commentMapper.addPhoneComment(comment.getCoId(),oId,commentName, coRtostate, coT);
        return integer;
    }


}
